package collections;

import util.Tracer;

public class SinglyLinkedList<T> {
	private SinglyLinkedListNode<T> head;
	
	public void insert(SinglyLinkedListNode<T> node)
	{
		node.setNext(head);
		head = node;
	}
	
	public void revese()
	{
		SinglyLinkedListNode<T> node = null;
		SinglyLinkedListNode<T> next = head;

		while(true)
		{
			SinglyLinkedListNode<T> nextNext = null;

			if(next == null)
			{
				head = node;
				break;
			}

			nextNext = next.getNext();
			next.setNext(node);

			node = next;
			next = nextNext;
		}
	}
	
	public static void main(String[] args) {
		SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();
		list.insert(new SinglyLinkedListNode<Integer>(1));
		list.insert(new SinglyLinkedListNode<Integer>(2));
		list.insert(new SinglyLinkedListNode<Integer>(3));
		list.insert(new SinglyLinkedListNode<Integer>(4));
		
		list.revese();
		
		SinglyLinkedListNode<Integer> node = list.head;
		while(node != null)
		{
			Tracer.trace(node.getKey());
			node = node.getNext();
		}
	}
}
